<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @framework : Codeigniter Framework
*
* @author: fatangel26
*
*/

	class Menu_model extends CI_Model
	{
	   protected $_table;

		public function __construct()
		{
			parent::__construct();

            $this->load->database();

            $this->_table = 'tbl_menu';
		}

        /*Basic Method*/

        public function select($data = array())
        {
            if(empty($data))
            {
                $query = $this->db->get($this->_table);

                if($query->num_rows()>0)
                    return $query->result_object();
            }
            else
            {
                if(isset($data['order_by']))
                {
                    $query = $this->db->select('menu_id, menu_name, menu_parent')
                                   ->order_by('menu_order', 'ASC')
                                   ->get($this->_table);
                    if($query->num_rows()>0)
                        return $query->result_object();
                }//End if
                elseif(isset($data['menu_id']))
                {
                    $query = $this->db->get_where($this->_table,$data);

                    if($query->num_rows()>0)
                        return $query->row();
                }
            }//End else

            return FALSE;
        }

        public function edit($menu_id,$data = array())
        {
            $data['slug'] = $this->create_unique_slug($data['menu_title'],$this->_table,'slug','menu_id', $menu_id);

            if(isset($data['menu_status']))
                $this->db->set('menu_status', $data['menu_status']);
            if(isset($data['menu_parent']))
                $this->db->set('menu_parent', $data['menu_parent']);
            if(isset($data['menu_name']))
                $this->db->set('menu_name', $data['menu_name']);
            if(isset($data['menu_order']))
                $this->db->set('menu_order', $data['menu_order']);

            $this->db->set('slug', $data['slug']);

            $this->db->where('menu_id', $menu_id);

            if($this->db->update($this->_table))
                return TRUE;
            return FALSE;

        }

        public function delete($menu_id)
        {
            $query = $this->db->delete($this->_table,array('menu_id' => $menu_id));

            if($this->db->affected_rows()>0)
                return TRUE;
            return FALSE;
        }

        public function create($data = array())
        {
            $data['slug'] = $this->create_unique_slug($data['menu_title'],$this->_table,'slug');

            $query = $this->db->insert($this->_table,$data);

            if($this->db->affected_rows()>0)
                return TRUE;
            return FALSE;
        }

        /*Magic Method*/

        protected function create_unique_slug($string, $table, $field='slug', $key=NULL, $value=NULL)
        {
            $t =& get_instance();
        	$slug = url_title($string);
        	$slug = strtolower($slug);
        	$i = 0;
        	$params = array ();
        	$params[$field] = $slug;


            if($key)
                $params["$key != "] = $value;

        	while ($t->db->where($params)->get($table)->num_rows())
        	{
        	    if (!preg_match ('/-{1}[0-9]+$/', $slug ))
        		    $slug .= '-' . ++$i;
        	    else
        		    $slug = preg_replace ('/[0-9]+$/', ++$i, $slug );

        	    $params [$field] = $slug;
        	}

        	return $slug;
        }//End function create_unique_slug

        private function check_has_child($menus, $parent_id)
        {
            foreach($menus as $menu)
            {
                if($menu->menu_parent == $parent_id)
                    return TRUE;
            }

            return FALSE;
        }

        public function build_tree($menus, $parent_id = 0, $level = 1)
        {

            $html = '';

            foreach($menus as $menu)
            {
                if($menu->menu_parent == $parent_id)
                {
                    $html .= "<tr>";

                    if($menu->menu_parent == 0)
                    {
                        $html .= "<td> + ".$menu->menu_name."</td>";
                    }
                    else
                    {
                        $html .= "<td style='padding-left:". $level*25 ."px!important'> →  ".$menu->menu_name."</td>";
                    }

                    $html .= "</tr>";

                    if($this->check_has_child($menus, $menu->menu_id))
                    {
                        $html .= $this->build_tree($menus, $menu->menu_id, $level+1);
                    }//Endif
                }//Endif
            }//End foreach

            return $html;
        }

        public function countChildren($menu_id)
        {
            if($menu_id != 0)
            {
                $strSQL = "SELECT COUNT(*) as NumsChild FROM ".$this->_table." WHERE menu_parent = ? GROUP BY menu_parent";

                $query = $this->db->query($strSQL, array('menu_id' => $menu_id));

                if($query->num_rows()>0)
                {
                    return $query->row()->NumsChild;
                }

            }

            return FALSE;

        }

        public function getMenus()
        {
            $query = $this->db->get_where($this->_table,array('menu_parent != '=>0));

            if($query->num_rows()>0)
                return $query->result_object();
            return FALSE;
        }



	}

/* End of file Menu_model.php */
/* Location: ./models/menu_model.php */